﻿//--------------------------------------------------------------------------
// 
//  Copyright (c) Chili Software.  All rights reserved. 
// 
//  File: StorageException.cs
//
//  Description: Expreion is thrown if a storage error happens.
// 
//--------------------------------------------------------------------------

using System;
using Chili.Opf3.Query;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Chili.Opf3.Storages
{
	/// <summary>
	/// Represents the base exception for all exceptions that are thrown by the storage
	/// classes.
	/// </summary>
	[Serializable]
	public class StorageException : Exception
	{
		private IQuery _query;

		/// <summary>
		/// Creates a new instance of the <see cref="StorageException">StorageException
		/// Class</see>.
		/// </summary>
		/// <param name="query">Query that has been executed before the exception has been thrown.</param>
		public StorageException(IQuery query)
		{
			_query = query;
		}

		/// <summary>
		/// Creates a new instance of the <see cref="StorageException">StorageException
		/// Class</see>.
		/// </summary>
		/// <param name="message">A message that describes the current exception.</param>
		/// <param name="query">Query that has been executed before the exception has been thrown.</param>
		public StorageException(string message, IQuery query) : base(message)
		{
			_query = query;
		}

		/// <summary>
		/// Creates a new instance of the <see cref="StorageException">StorageException
		/// Class</see>.
		/// </summary>
		/// <param name="message">A message that describes the current exception.</param>
		/// <param name="inner">The exception that caused the current exception.</param>
		/// <param name="query">Query that has been executed before the exception has been thrown.</param>
		public StorageException(string message, System.Exception inner, IQuery query) : base(message, inner)
		{
			_query = query;
		}

		/// <summary>
		/// Creates a new instance of the
		/// <see cref="StorageException">StorageException Class</see>.
		/// </summary>
		protected StorageException(SerializationInfo info, StreamingContext context) : base(info, context)
		{
			_query = (IQuery)info.GetValue("query", typeof(IQuery));
		}

		/// <summary>
		/// Restores the objects data after serialization.
		/// </summary>
		[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("query", _query);
		}

		/// <summary>Query that has been executed before the exception has been thrown.</summary>
		/// <value>
		/// Returns the <see cref="Opf3.Query.IQuery">IQuery</see> that has been executed
		/// before the exception has been thrown.
		/// </value>
		public IQuery Query
		{
			get { return _query; }
		}
	}
}
